class Solution {
public:
    int rob(vector<int>& nums) {
        if(nums.size() == 1)return nums[0];
        if(nums.size() == 2)return max(nums[0], nums[1]);
        int pre = max(nums[0], nums[1]), prepre = nums[0];
        for(int i = 2; i < nums.size() - 1; i++){
            int temp = max(pre, prepre + nums[i]);
            prepre = pre;
            pre = temp;
        }
        int ans1 = pre;
        pre = nums[1];
        prepre = 0;
        for(int i = 2; i < nums.size(); i++){
            int temp = max(pre, prepre + nums[i]);
            prepre = pre;
            pre = temp;
        }
        return max(pre, ans1);
    }
};
//2021.4.15 


